﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace BD2MKV
{
    class TheTVDB
    {
        public static List<Episode> series = new List<Episode>();
        public static string tvdbApiKey = "F2273CDBB03105B3";

        public static void LoadSeries(string seriesId, string language)
        {
            WebClient webFile = new WebClient();
            string sysTempDir = System.IO.Path.GetTempPath();
            webFile.DownloadFile("http://www.thetvdb.com/api/" + tvdbApiKey + "/series/" + seriesId + "/all/" + language + ".zip", sysTempDir + language + ".zip");


            using (ZipInputStream s = new ZipInputStream(File.OpenRead(sysTempDir + language + ".zip")))
            {

                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {

                    Console.WriteLine(theEntry.Name);

                    string directoryName = sysTempDir + Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    // create directory
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(directoryName + theEntry.Name))
                        {

                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            XmlTextReader reader = new XmlTextReader(sysTempDir + language + ".xml");
            string tmpSeasonNumber = "";
            string tmpEpisodeName = "";
            string tmpEpisodeNumber = "";
            string tmpEpisodeId = "";
            string tmpSeriesId = "";

            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "Episode")
                    break;

            while (reader.Read())
            {
                
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");

                        switch (reader.Name)
                        {
                            case "id":
                                reader.Read(); // Episode Id
                                tmpEpisodeId = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "SeasonNumber":
                                reader.Read(); // Season Number
                                tmpSeasonNumber = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "EpisodeName":
                                reader.Read(); // language
                                tmpEpisodeName = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "EpisodeNumber":
                                reader.Read(); // language
                                tmpEpisodeNumber = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "seriesid":
                                reader.Read(); // language
                                tmpSeriesId = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            default:
                                break;
                        }

                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        switch (reader.Name)
                        {
                            case "Episode":
                                Episode ep = new Episode();
                                ep.episodeId = tmpEpisodeId;
                                ep.episodeName = tmpEpisodeName;
                                ep.episodeNumber = tmpEpisodeNumber;
                                ep.seasonNumber = tmpSeasonNumber;
                                ep.seriesId = tmpSeriesId;
                                series.Add(ep);

                                tmpEpisodeId = "";
                                tmpEpisodeName = "";
                                tmpEpisodeNumber = "";
                                tmpSeasonNumber = "";
                                tmpSeriesId = "";

                                break;
                            default:
                                break;
                        }
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    default:
                        Console.WriteLine(reader.Value);
                        break;
                }
            }
        }
    }
}
